/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_Studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "构建脚本节点.h"
#include <节点/逻辑/单节点脚本.h>


static void on_函数节点_参数修改(S_Props& prop) {
	//switch ((*(S_PropValueEnum*)prop.m_Value).m_当前选项)
	auto nodes = f_global_get激活节点();
	for (auto 节点 : nodes) {
		C_函数节点* node = dynamic_cast<C_函数节点*>(节点);
		if (node) {
			auto* t = node->f_get子集树();

			E_对话框类型 类型;
			if (f_prop_Bool(t->m_外部加载)) {
				类型 = E_对话框类型::e_读取;
				f_ui_set路径编辑框对话框后缀(prop.m_UI->m_部件, ".bc");
			}
			else {
				类型 = E_对话框类型::e_写入;
				f_ui_set路径编辑框对话框后缀(prop.m_UI->m_部件, ".hpp");
			}
			f_ui_set路径编辑框对话框类型(prop.m_UI->m_部件, 类型);
			
		}

		节点->f_set是否要更新(true);
	}
}





static void f_函数节点_3D视口更新(C_节点面板* node, S_Scene* scene) {
	C_函数节点* 网格节点 = dynamic_cast<C_函数节点*>(node->m_NodeData);

}

static void f_函数节点_部件构建属性(C_属性栏* plane, S_结构对象指针 obj) {
	C_函数节点* node = dynamic_cast<C_函数节点*>(((C_节点面板*)obj)->m_NodeData);

	std::vector<S_Props> props;
	f_节点重构_重构属性面板数据((C_节点面板*)obj, props);


	//node->m_外部加载.m_UI->m_Update = on_函数节点_参数修改;
	//props.push_back(node->m_外部加载);
	//node->m_代码文件路径.m_UI->m_Update = on_函数节点_参数修改;
	//props.push_back(node->m_代码文件路径);
	//node->m_函数名称.m_UI->m_Update = on_函数节点_参数修改;
	//props.push_back(node->m_函数名称);

	auto* t = node->f_get子集树();

	node->m_编译运行.m_UI->m_Update = on_函数节点_参数修改;
	props.push_back(node->m_编译运行);

	t->m_外部加载.m_UI->m_Update = on_函数节点_参数修改;
	props.push_back(t->m_外部加载);
	if (f_prop_Bool(t->m_外部加载)) {
		t->m_代码文件路径.m_UI->m_Update = on_函数节点_参数修改;
		props.push_back(t->m_代码文件路径);
	}
	//t->m_函数名称.m_UI->m_Update = on_函数节点_参数修改;
	//props.push_back(t->m_函数名称);
	//props.push_back(node->m_链接的节点树名称);

	plane->f_构建属性部件(props);

}

C_节点面板* f_节点构建_函数(S_UI创建环境& ctx, S_设备环境& dev_ctx, C_节点基类* node, C_节点树* tree) {
	if (!node) {
		node = new C_函数节点(dev_ctx, tree);
		node->m_ICO = "ico函数脚本0017";

		for (auto& e : node->f_get子集树()->m_Nodes) {
			e->m_坐标 += S_框架::g节点创建初始位置;
		}
	}
	C_节点面板* plane = new C_节点面板(node, ctx, E_节点区类型::e_节点区_物体);
	if (plane) {
		if (!plane->m_同子集节点树关联UI部件) {
			plane->m_同子集节点树关联UI部件 = new std::set<C_Widget*>();
		}
		(*plane->m_同子集节点树关联UI部件).insert(plane);
	}
	
	f_节点重置_函数(plane, node);
	return plane;
}

void f_节点重置_函数(C_节点面板* plane, C_节点基类* node) {
	f_节点重构_面板插座重构(plane, node, E_节点区类型::e_节点区_节点视图);
	plane->m_属性面板参数构建 = f_函数节点_部件构建属性;
	plane->m_ICO = node->m_ICO;

	//if (static_cast<C_函数节点*>(node)->f_getIsLink()) {
	if (plane->m_同子集节点树关联UI部件 == nullptr) {
		plane->m_同子集节点树关联UI部件 = new std::set<C_Widget*>();
		(*plane->m_同子集节点树关联UI部件).insert(plane);
	}


	//}
}





static void f_Python节点_部件构建属性(C_属性栏* plane, S_结构对象指针 obj) {
	C_Python节点* node = dynamic_cast<C_Python节点*>(((C_节点面板*)obj)->m_NodeData);

	std::vector<S_Props> props;
	plane->f_构建属性部件(props);

	f_函数节点_3D视口更新(((C_节点面板*)obj), nullptr);
}

C_节点面板* f_节点构建_Python(S_UI创建环境& ctx, S_设备环境& dev_ctx, C_节点基类* node, C_节点树* tree) {
	if (!node) node = new C_Python节点(dev_ctx);
	//if (nullptr == plane) {
	//	plane = f_节点创建_UI面板构建(ctx, node, E_节点区类型::e_节点区_节点视图);
	//	plane->m_属性面板参数构建 = f_Python节点_部件构建属性;
	//}
	C_节点面板* plane = new C_节点面板(node, ctx, E_节点区类型::e_节点区_物体);
	f_节点重置_Python(plane, node);
	
	return plane;
}

void f_节点重置_Python(C_节点面板* plane, C_节点基类* node) {
	f_节点重构_面板重构(plane, node, E_节点区类型::e_节点区_节点视图);
	f_节点重构_插座重构(plane, node, E_节点区类型::e_节点区_节点视图);

	plane->m_属性面板参数构建 = f_Python节点_部件构建属性;
	plane->m_3D视口更新 = f_函数节点_3D视口更新;
	plane->m_ICO = "ico函数脚本0017";
}







bool f_脚本节点重构(C_节点面板** plane, C_节点基类* node, S_UI创建环境* ctx)
{
	if (node->m_TypeName == "函数脚本节点") {
		if (!plane[0]) plane[0] = f_节点构建_函数(*ctx, *S_框架::g_3D视口环境, node);
		f_节点重置_函数(plane[0], node);
	}
	else if (node->m_TypeName == "函数脚本节点") {
		if (!plane[0]) plane[0] = f_节点构建_函数(*ctx, *S_框架::g_3D视口环境, node);
		f_节点重置_函数(plane[0], node);
	}
	else {
		return false;
	}

	return true;
}








